﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Diagnostics;


namespace depl
{
    using System;
    using System.Windows.Data;
    using System.Windows;
    using System.Windows.Controls.Primitives;

    public partial class QueryBox : UserControl
    {
        public bool isShowingHistory { set; get; }
        public bool historyShown { set; get; }
        public HistoryItem HistorySelectedItem = null;
        public int QueryPopupHeight 
        { 
            get 
            { 
                return 100; 
            } 
            set
            {
                return;
            }
        }

        bool AllPassItemFilter(string search, object value)
        {
            return true;
        }

        bool inputShown = true;
        public QueryBox()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public void AnimateShowInput()
        {
            if (inputShown)
                return;
            
            FadeInBackground.Begin();
            
        }
        public void AnimateHideInput()
        {
            if (!inputShown)
                return;
            AnimationHide.From = 0;
            AnimationHide.To = LayoutRoot.ActualWidth;
            HideInput.Begin();
        }

        private void HideInputCompleted(object sender, EventArgs e)
        {
            inputShown = false;
            Input.Visibility = Visibility.Collapsed;
            ClearButton.Visibility = Visibility.Collapsed;
            HistoryButton.Visibility = Visibility.Collapsed;
            BackgroundRect.Visibility = Visibility.Collapsed;
            FadeOutBackground.Begin();
        }

        private void FadeInBackgroundCompleted(object sender, EventArgs e)
        {
            inputShown = true;
            Input.Visibility = Visibility.Visible;
            BackgroundRect.Visibility = Visibility.Visible;
            HistoryButton.Visibility = Visibility.Visible;
            ClearButton.Visibility = Visibility.Visible;
            AnimationShow.From = LayoutRoot.ActualWidth;
            AnimationShow.To = 0;
            ShowInput.Begin();

        }

        private void TextGotFocus(object sender, RoutedEventArgs e)
        {
            BackgroundRect.Stroke = (SolidColorBrush)this.Resources["AccentBorderBrush"];
            BackgroundRect.Fill = (SolidColorBrush)this.Resources["WhiteBackgroundBrush"];
        }

        private void TextLostFocus(object sender, RoutedEventArgs e)
        {
            BackgroundRect.Stroke = (SolidColorBrush)this.Resources["GrayBackgroundBrush"];
            BackgroundRect.Fill = (SolidColorBrush)this.Resources["GrayBackgroundBrush"];
        }

        private void AutoCompleteBox_TextChanged(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("text changed: "+Input.Text);
            
            if (historyShown)
            {
                HistorySelectedItem = this.Input.SelectedItem as HistoryItem;
                Debug.WriteLine("Exiting history view");
                this.Input.TextChanged -= AutoCompleteBox_TextChanged;


                //this.Input.

                this.Input.ItemFilter = PivotPage1.ItemFilter;
                this.Input.ItemsSource = null;
                Debug.WriteLine("isshowing history to false");
                isShowingHistory = false;
            }
            
        }

        private void HistoryOnClick(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Clicked history");

            List<HistoryItem> items = EntryStorage.Instance.GetHistory();
            if (items.Count == 0)
            {
                Debug.WriteLine("No history");
                return;
            }
            HistorySelectedItem = null;
            isShowingHistory = true;
            historyShown = false;
            this.Input.ItemsSource = items;
            this.Input.ItemFilter = AllPassItemFilter;
            this.Input.TextChanged += AutoCompleteBox_TextChanged;
            
            this.Input.MinimumPrefixLength = 0;
            this.Input.MinimumPopulateDelay = 0;

            Debug.WriteLine("Showing history");

            
            foreach (HistoryItem entry in this.Input.ItemsSource)
            {
                Debug.WriteLine(entry.Name);
            }

            //trigger populating event
            this.Input.Focus();
            DependencyObject o = VisualTreeHelper.GetChild(Input, 0);
            o = VisualTreeHelper.GetChild(o, 0);
            ((TextBox)(o)).Text = ".";
            ((TextBox)(o)).Text = "";
            
        }




        private void ClearOnClick(object sender, RoutedEventArgs e)
        {
            this.Input.Text = "";
            DependencyObject o = VisualTreeHelper.GetChild(Input, 0);
            o = VisualTreeHelper.GetChild(o, 0);
            ((TextBox)(o)).Text = "";
            this.Input.Focus();
        }

        private void OnPopupOpened(object sender, EventArgs e)
        {
            if (isShowingHistory)
            {
                Debug.WriteLine("popup opened");
                this.Input.MinimumPrefixLength = PivotPage1.MIN_PREFIX_LENGTH;
                this.Input.MinimumPopulateDelay = PivotPage1.MIN_POPULATE_DELAY;
                historyShown = true;
            }
        }

    }
}
